﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcRags.Core
{
    public enum FlashType
    {
        Debug,
        Notice,
        Alert
    }  

    public static class FlashHelper
    {
        public static void Flash(this Controller controller, FlashType type, string message)
        {
            if (!controller.TempData.ContainsKey("flashMessages"))
                controller.TempData["flashMessages"] = new Dictionary<FlashType, List<string>>();

            Dictionary<FlashType, List<string>> flashMessageDict = (Dictionary<FlashType, List<string>>)controller.TempData["flashMessages"];
            if(!flashMessageDict.ContainsKey(type))
                flashMessageDict.Add(type, new List<string>());

            List<string> flashMessages = flashMessageDict[type];
            flashMessages.Add(message);
        }

        public static List<string> GetFlashMessages(this HtmlHelper htmlHelper, FlashType type)
        {
            List<string> messages = new List<string>();
            if (htmlHelper.ViewContext.TempData.ContainsKey("flashMessages"))
            {
                Dictionary<FlashType, List<string>> flashMessageDict = (Dictionary<FlashType, List<string>>)htmlHelper.ViewContext.TempData["flashMessages"];
                if (flashMessageDict.ContainsKey(type))
                    messages = flashMessageDict[type];
            }
            return messages;
        }

    }
}